package com.test.burnincash.activity_fragment_burnin.login_burnin;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;

import androidx.annotation.NonNull;

import com.test.burnincash.R;
import com.test.burnincash.activity_fragment_burnin.ApplyPermissionActivity_burnin;
import com.test.burnincash.activity_fragment_burnin.base_burnin.BaseLoginActivity_burnin;
import com.test.burnincash.bean_burnin.config_info_burnin.AppShowInfo_burnin;
import com.test.burnincash.bean_burnin.user_info_burnin.LoginInfo_burnin;
import com.test.burnincash.util_burnin.PermissionUtil_burnin;
import com.test.burnincash.util_burnin.StringUtil_burnin;
import com.test.burnincash.util_burnin.helper_burnin.UserInfoHelper_burnin;
import com.test.burnincash.util_burnin.http_burnin.HttpManager_burnin;

import org.json.JSONObject;


public class LoginPhoneNumActivity_burnin extends BaseLoginActivity_burnin {

    private EditText phoneNumEt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_phone_num_burnin);
        init();
    }

    @SuppressLint("SetTextI18n")
    private void init() {

        phoneNumEt = findViewById(R.id.activity_login_phone_num_et_phone_num);

        phoneNumEt.setHint(AppShowInfo_burnin.getInstance().getMobile());

        findViewById(R.id.activity_login_phone_num_layout_next).setOnClickListener(v -> {
            if (TextUtils.isEmpty(phoneNumEt.getText())) {
                showToast_burnin(getString(R.string.word_input_tip));
                return;
            }

            String phoneNum = StringUtil_burnin.getSafeString(phoneNumEt.getText().toString());

            //测试账号登录
            String accountToken = AppShowInfo_burnin.getInstance().isTestAccount(phoneNum);
            if (!TextUtils.isEmpty(accountToken)) {
                try {
                    JSONObject object = new JSONObject();
                    object.put("token", accountToken);
                    object.put("userId", phoneNum);
                    object.put("typ", UserInfoHelper_burnin.LOGIN_TYPE_OLD);
                    LoginInfo_burnin.parse(object);
                    onUserLoginResponse_burnin();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return;
            }

            HttpManager_burnin.getInstance().requestOtpCode(phoneNum);
        });

        //ASAduidManager.getInstance().initAduid();//初始化设备id
        //检查权限是否全部同意
        String[] permissions = AppShowInfo_burnin.getInstance().getPermissions();
        if (!PermissionUtil_burnin.checkLaunchPermissions(this, permissions)) {
            startActivityForSingleTop_burnin(ApplyPermissionActivity_burnin.class);
            return;
        }
        //检查是否已登录
        if (UserInfoHelper_burnin.getInstance().didLogin()) {

            phoneNumEt.setText(UserInfoHelper_burnin.getInstance().getMobile());

            login();
        }

    }

    @Override
    public void onRequestOtpCodeResponse_burnin(String msg,@NonNull JSONObject jsonObject) {

        String directLoginFlag = StringUtil_burnin.getSafeString(jsonObject.optString("directLoginFlag"));

        //不需要验证码直接登录
        if (directLoginFlag.equalsIgnoreCase("YES")) {
            HttpManager_burnin.getInstance().userLoginOldUser(StringUtil_burnin.getSafeString(phoneNumEt.getText().toString()));
            return;
        }

        UserInfoHelper_burnin.getInstance().setMobile(phoneNumEt.getText().toString());
        startActivity_burnin(LoginVerCodeActivity_burnin.class);
    }
}